{include file="layout/layout" /}

<script type="text/javascript" charset="utf-8" src="__ROOT__public/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__ROOT__public/ueditor/ueditor.all.min.js"> </script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="__ROOT__public/ueditor/lang/zh-cn/zh-cn.js"></script>

<body style="background-color: rgb(255, 255, 255); overflow: auto;" class="laydate_body">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page" style="">
  <div class="fixed-bar">
    <div class="item-title"><a class="back" href="javascript:history.back();" title="返回列表"><i class="fa fa-arrow-circle-o-left"></i></a>
      <div class="subject">
        <h3>文章管理 - {if $articleid.art_cat_id==''}新增文章{else/}修改文章{/if}
        </h3>
        <h5>网站系统文章索引与管理</h5>
      </div>
    </div>
  </div>
  <form class="form-horizontal" action="" id="add_post" method="post" enctype="multipart/form-data">
    <input id="old_art_img" type="hidden" name="old_art_img" value="{$articleid.art_img}">
    <input id="art_id" type="hidden" name="art_id" value="{$articleid.art_id}">

    <div class="ncap-form-default" style="">
      <dl class="row">
        <dt class="tit">
          <label for="cat_id">所属分类</label>
        </dt>
        <dd class="opt">
        <select class="small form-control" name="art_cat_id" id="art_cat_id">
            <option value="" >选择分类</option>
            {foreach $articlecat as $v}
            <option {$v.cat_id == $articleid.art_cat_id ? 'selected' : ''} value={$v.cat_id} >{$v.cat_name}</option>
            {/foreach}
        </select>
          <span class="err" id="err_cat_id"></span>
          <p class="notic">请正确选择文章所属分类</p>
        </dd>
      </dl>
      <dl class="row">
        <dt class="tit">
          <label>文章标题</label>
        </dt>
        <dd class="opt">
          <input id="art_title" name="art_title" class="input-txt" type="text" value="{$articleid.art_title}">
          <span class="err" id="err_title"></span>
          <p class="notic">标明文章、作品等内容的简短语句</p>
        </dd>

      </dl>
	  <dl class="row">
        <dt class="tit">
          <label for="articleForm">文章作者</label>
        </dt>
        <dd class="opt">
          <input id="art_author" name="art_author" class="input-txt" type="text" value="{$articleid.art_author}">
          <span class="err"></span>
          <p class="notic">作品的创作者</p>
        </dd>
      </dl>
      <dl class="row">
        <dt class="tit">
          <label for="articleForm">文章摘要</label>
        </dt>
        <dd class="opt">
          <input id="art_desc" name="art_desc" class="input-txt" type="text" value="{$articleid.art_desc}">
          <span class="err"></span>
          <p class="notic">简明、确切地记述文献重要内容的短文。</p>
        </dd>
      </dl>
      <dl class="row">
        <dt class="tit">
          <label for="articleForm">缩略图片</label>
        </dt>
        <dd class="opt">
          <img id="show_art_img" width="100px" {if $articleid['art_img'] == ''} style="display:none;" {/if} onclick="showBigImg(this)" src="{$articleid['art_img'] != '' ? '__ROOT__'.$articleid['art_img'] :'' }" />
          <input id="art_img" name="art_img" class="input-txt" type="file">
          <span class="err"></span>
          <p class="notic">文章配图格式为jpg,png大小为2M。</p>
        </dd>
      </dl>
      <dl class="row" style="">
        <dt class="tit">
          <label>文章内容</label>
        </dt>
        <dd class="opt" style="">
            <textarea class="span12 ckeditor" id="art_content" name="art_content" title="">{$articleid.art_content}</textarea>
            <span class="err" id="err_content"></span>
            <p class="notic"></p>
        </dd>
      </dl>
      <div class="bot"><a href="javascript:void(0);" class="ncap-btn-big ncap-btn-green" id="submitBtn">确认提交</a></div>
    </div>
  </form>
</div>
<script type="text/javascript">
$("#art_img").change(function() {
    var objUrl = getObjectURL(this.files[0]);
    if (objUrl) {
        $("#show_art_img").attr("src", objUrl);
        $('#show_art_img').attr('style', 'display:block');
    }
});

function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) {
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) {
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) {
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

//实例化文本编辑器
var editor;
$(function() {
    //具体参数配置在  editor_config.js 中
    var options = {
        zIndex: 999,
        initialFrameWidth: "80%",
        //初化宽度
        initialFrameHeight: 400,
        //初化高度
        focus: false,
        //初始化时，是否让编辑器获得焦点true或false
        maximumWords: 99999,
        removeFormatAttributes: 'class,style,lang,width,height,align,hspace,valign',
        //允许的最大字符数 'fullscreen',
        pasteplain: false,
        //是否默认为纯文本粘贴。false为不使用纯文本粘贴，true为使用纯文本粘贴
        autoHeightEnabled: true
    };
    editor = new UE.ui.Editor(options);
    editor.render("art_content");
});

//验证表单是否传值
$('#submitBtn').click(function() {
    if ($('#art_cat_id').val() == '') {
        layer.alert("请选择文章类别！", {
            icon: 2
        });
        return false;
    }
    if ($('#art_title').val() == '') {
        layer.alert("请填写文章标题！", {
            icon: 2
        });
        return false;
    }
    if ($('#art_author').val() == '') {
        layer.alert("请选择文章作者！", {
            icon: 2
        });
        return false;
    }
    if ($('#art_desc').val() == '') {
        layer.alert("请填写文章摘要！", {
            icon: 2
        });
        return false;
    }
    if ($('#show_art_img').attr('src') == '') {
        layer.alert("请上传文件", {
            icon: 2
        });
        return false;
    }
    if (UE.getEditor('art_content').getContent() == '') {
        layer.alert("请填写文章内容！", {
            icon: 2
        });
        return false;
    }
    articleAddEdit();
});
//提交
function articleAddEdit() {
    layer.confirm("确定{if $articleid.art_cat_id==''}发布{else/}修改{/if}文章吗？", {
        btn: ['确定', '取消'],
        icon: 3
    },
    function() {
        var form = new FormData(document.getElementById("add_post"));
        var oldfilename = '';
        $.ajax({
            url: "{:url('Article/articleAddEdit')}",
            type: "post",
            data: form,
            cache: false,
            processData: false,
            contentType: false,
            success: function(data) {
                if (data.info == 1) {
                    layer.msg(data.msg, {
                        time: 2000,
                        icon: 2
                    });
                }
                if (data.info == 2) {
                    layer.msg(data.msg, {
                        time: 2000,
                        icon: 1
                    },
                    function() {
                        self.location = document.referrer;
                    });
                }
                if (data.info == 3) {
                    layer.msg("图片上传失败,原因是:" + data.msg, {
                        time: 2000,
                        icon: 2
                    });
                }
                if (data.info == 4) {
                    layer.msg(data.msg, {
                        time: 2000,
                        icon: 2
                    });
                }
            },
            error: function() {
                layer.msg('服务器错误,请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }
        });
    },
    function() {
        layer.msg('取消成功!', {
            time: 2000,
            icon: 0
        });
    });
}
</script>
